package com.bigchun.seeland;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import bean.ContentDataBean;
import myapp.MyApp;
import myview.EmojiEditText;
import util.MyLog;
import util.RequestManager;
import util.SystemStatusManager;

public class ChangeNameActivity extends BaseActivity {


    private LinearLayout ll_reback;
    private TextView tv_save;
    private EmojiEditText et_name;
    private Intent intent = null;
    private Context context;
    private String token;
    private MyApp myapp;
    private String nickname;
    private String apptype = "";
    private String textData;
    private ContentDataBean.DataBean textContent;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_changename);
        setTranslucentStatus();
        context = this;
        myapp = (MyApp) getApplicationContext();
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        apptype = sharedPreferences.getString("apptype", "0");
        nickname = sharedPreferences.getString("nickname", "");
        textData = sharedPreferences.getString("textData", "");
        initView();
        initLin();


    }


    private void initView() {
        ll_reback = findViewById(R.id.ll_reback);
        tv_save = findViewById(R.id.tv_save);
        et_name = findViewById(R.id.et_name);
        et_name.setText(nickname);
        et_name.setSelection(nickname.length());
        TextView tv_title = findViewById(R.id.tv_title);
        TextView tv_name_des = findViewById(R.id.tv_name_des);
        textContent = new Gson().fromJson(textData, new TypeToken<ContentDataBean.DataBean>() {}.getType());
        if (textContent != null){
            tv_title.setText(textContent.getChangename());
            tv_save.setText(textContent.getSave());
            tv_name_des.setText(textContent.getGoodname());
        }
    }


    private void initLin() {
        ll_reback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String nameEt = et_name.getText().toString().trim();
                saveNickname(nameEt);
            }
        });

    }

    /**
     * 保存昵称
     */
    private void saveNickname(String name) {
Log.e("-----------",name+"    昵称");
        String url = myapp.getWebConfig() + "/api/user/profile";
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", token);
        paramsMap.put("nickname", name);
        paramsMap.put("is_material", apptype);
        RequestManager.getInstance(context).requestAsyn(url, RequestManager.TYPE_POST_FORM, paramsMap, new RequestManager.ReqCallBack() {
            @Override
            public void onReqSuccess(Object result) {
                MyLog.e("code--result", result.toString());
                Message msg = new Message();
                try {
                    JSONObject jsonObject = new JSONObject(result.toString());
                    if (jsonObject.getString("code").equals("0")) {

                        msg.obj = jsonObject.getString("msg");
                        msg.arg1 = 0;
                        handler.sendMessage(msg);
                        return;
                    }


                    msg.obj = jsonObject.getString("msg");
                    msg.arg1 = 3;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    msg.arg1 = 1;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                MyLog.e("result", errorMsg);
                if (errorMsg.equals("未登录")){
                    Message msg = new Message();
                    msg.arg1 = 11;
                    handler.sendMessage(msg);
                }
            }
        });
    }
    public Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.arg1) {
                case 0:
                    String str = (String) msg.obj;
                    Toast.makeText(context, str, Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    if (textContent != null){
                        Toast.makeText(context, textContent.getNet_error(), Toast.LENGTH_LONG).show();
                    }
                    break;
                case 2:

                    break;
                case 3:
                    SharedPreferences spuserInfo = getSharedPreferences("userInfo", Activity.MODE_PRIVATE);
                    String nameEt = et_name.getText().toString().trim();
//                    spuserInfo.edit().putString("username",userinfo.getString("username")).commit();
                    spuserInfo.edit().putString("nickname",nameEt).commit();
                    String asa = (String) msg.obj;
//                    Toast.makeText(context, asa, Toast.LENGTH_LONG).show();
                    finish();
                    break;
                case 4:

                    break;
                case 11:
                    if (!islogin){
                        islogin = true;
                        sharedPreferences.edit().putString("token", "").commit();
                        Intent intent = new Intent(context, LoginActivity.class);
                        context.startActivity(intent);
                        myapp.destoryActivity("主页面");
                    }
                    break;
            }
        }
    };
private boolean islogin = false;
    /**
     * 设置状态栏背景状态
     */
    public  void setTranslucentStatus()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window win =  getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }
        SystemStatusManager tintManager = new SystemStatusManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(0);//状态栏无背景
    }

}
